
from PyQt4.QtGui import QTableView
from PyQt4.QtCore import Qt, QAbstractTableModel, QVariant, SIGNAL

try:
    from numpy import ndarray, dtype
except ImportError:
    ndarray, dtype = None, None

class SCArrayModel(QAbstractTableModel):
    
    def __init__(self,data,parent=None):
        QAbstractTableModel.__init__(self,parent)
        assert data.dtype in (dtype('int32'),dtype('float64'))
        self.__data = data.copy()
        self.__dtype = data.dtype

    def getData(self):
        return self.__data
    
    def rowCount(self):
        return self.__data.shape[0]
    
    def columnCount(self):
        if len(self.__data.shape) > 1:
            return self.__data.shape[1]
        return 1
    
    def recordData(self,index,value):
        if self.__dtype == dtype('int32'):
            val, ok = value.toDouble()
        elif self.__dtype == dtype('float64'):
            val, ok = value.toInt()
        return False
    
    def setData(self, index, value, role=Qt.EditRole):
        if not index.isValid():
            return False
        if role == Qt.EditRole and self.recordData(index,value):
            self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"),index,index)
            return True
        return False
    
    def data(self,index, role=Qt.DisplayRole):
        if not index.isValid():
            return QVariant()
        return QVariant()
    
class SCArrayEditor(QTableView):
    
    def __init__(self,data,parent=None):
        QTableView.__init__(self,parent)
        self.setModel(SCArrayModel(data))

    